package cn.cloud.service;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.cleverframe.fastdfs.client.StorageClient;
import org.cleverframe.fastdfs.model.MateData;
import org.cleverframe.fastdfs.model.StorePath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Set;

/**
 * 上传图片到Fast
 *
 */
@Service
public class FastDFSClient {
    @Autowired(required = false)
    private StorageClient storageClient;

    /**
     * 上传文件到fdfs,如果上传失败则path返回null
     * @param is 文件输入流
     * @param name 文件名
     * @param size 文件大小
     * @param metaDataSet 元数据
     * @return
     */
    public String uploadFile(InputStream is, String name, long size, Set<MateData> metaDataSet) {
        String path = null;
        try {
            String ext = FilenameUtils.getExtension(name);

            //  group1/M00/00/01/wKjIgFWOYc6APpjAAAD-qk29i78248.jpg
            StorePath storePath = storageClient.uploadFile(is, size, ext, metaDataSet);
            path = storePath.getFullPath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

    /**
     * 上传文件到fdfs,如果上传失败则返回path为null
     * @param file 上传文件
     * @param metaDataSet 元数据
     * @return
     */
    public String uploadFile(File file, Set<MateData> metaDataSet) {
        String path = null;
        try {
            String name = file.getName();
            long size = file.length();
            FileInputStream fis = FileUtils.openInputStream(file);
            path = uploadFile(fis, name, size, metaDataSet);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return path;
    }

    /**
     * 上传文件到fdfs,如果上传失败则返回path为null
     * @param file 上传文件
     * @return
     */
    public String uploadFile(File file) {
        String path = null;
        try {
            path = uploadFile(file, null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

    /**
     * 删除文件
     * @param fid uploadFile返回的路径
     * @return
     */
    public boolean deleteFile(String fid) {
        String groupName = getGroupByFid(fid);
        String path = getPathByFid(fid);
        return storageClient.deleteFile(groupName, path);
    }

    /**
     * 根据fid返回元数据
     * @param fid
     * @return
     */
    public Set<MateData> getMetadata(String fid) {
        String groupName = getGroupByFid(fid);
        String path = getPathByFid(fid);
        return storageClient.getMetadata(groupName, path);
    }

    /**
     * 根据fid返回groupName
     * @param fid
     * @return
     */
    public String getGroupByFid(String fid) {
        return StringUtils.substringBefore(fid, "/");
    }

    /**
     * 根据fid返回文件path
     * @param fid
     * @return
     */
    public String getPathByFid(String fid) {
        return StringUtils.substringAfter(fid, "/");
    }
}
